package gx.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

public class Formats {

	public static String toString( Object a ) {
		return nullto( a );
	}

	// 把null转化成空
	public static String nullto( Object a ) {
		/*
        if ( a instanceof oracle.sql.CLOB ) {
            try {
                return ( ( oracle.sql.CLOB ) a ).stringValue();
            }
            catch ( SQLException e ) {
                e.printStackTrace();
            }
        }
        */
		return a == null ? "" : a.toString().trim();
	}


	public static int toInt( Object a ) {
		int r = 0;
		if ( a == null || a.toString().equals( "" ) ) {
			r = 0;
		}
		else {
			try {
				r = Integer.parseInt( a.toString().trim() );
			}
			catch ( Exception e ) {
				r = 0;
			}
		}
		return r;
	}


	/**
	 * double to int，四舍五入
	 * 
	 * @param a
	 * @return
	 */
	public static int doubleToInt( Object a ) {
		int r = 0;
		if ( a == null ) {
			r = 0;
		}
		else {
			try {
				r = Integer.parseInt( new java.text.DecimalFormat( "0" ).format( a ) );
			}
			catch ( Exception e ) {
				r = 0;
				e.printStackTrace();
			}
		}
		return r;
	}


	public static double toDouble( Object a ) {
		double r = 0;
		if ( a == null || a.toString().equals( "" ) ) {
			r = 0;
		}
		else {
			try {
				r = Double.parseDouble( a.toString() );
			}
			catch ( Exception e ) {
				r = 0;
			}
		}
		return r;
	}


	// 把数组null元素转化成空
	public static String[] nullto( String[] a ) {
		if ( a == null ) {
			a = new String[0];
		}
		else {
			for ( int i = 0; i < a.length; i++ ) {
				a[ i ] = a[ i ] == null ? "" : a[ i ];
			}
		}
		return a;
	}


	// 数组去重复
	public static String[] arrayUnDup( String[] array ) {
		List< String > l = ( List< String > ) new ArrayList< String >();
		for ( int i = 0; i < array.length; i++ ) {
			if ( !l.contains( array[ i ] ) ) {
				l.add( array[ i ] );
			}
		}
		String[] a = new String[l.size()];
		for ( int i = 0; i < l.size(); i++ ) {
			a[ i ] = l.get( i );
		}
		return a;
	}


	public static String join( String[] array, String contact ) {
		String a = "";
		for ( int i = 0; i < array.length; i++ ) {
			a += contact + array[ i ];
		}
		a = a.startsWith( contact ) ? a.substring( contact.length() ) : a;
		return a;
	}


	public static String[] initArray( String[] array ) {
		for ( int i = 0; i < array.length; i++ ) {
			array[ i ] = "";
		}
		return array;
	}


	public static boolean contains( String[] array, String element ) {
		boolean b = false;
		for ( int i = 0; i < array.length; i++ ) {
			if ( element.equals( array[ i ] ) ) {
				b = true;
				i = array.length;
				break;
			}
		}
		return b;
	}


	/**
	 * 传入时间字符串，返回时间类型
	 * 
	 * @param date
	 *            时间字符串目前只支持"yyyy-MM-dd HH:mm:ss"和"yyyy-MM-dd/HH:mm:ss"格式
	 * @return Date
	 */
	public static Date toDate( Object dateStr ) {
		String date = Formats.nullto( dateStr );
		SimpleDateFormat sdf;
		Date d = null;
		try {
			if ( Pattern.compile( "^\\d{4}[-]\\d{1,2}[-]\\d{1,2}[\\s]\\d{1,2}[:]\\d{1,2}[:]\\d{1,2}" ).matcher( date ).matches() ) {
				sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
				d = sdf.parse( date );
			}
			else if ( Pattern.compile( "^\\d{4}[-]\\d{1,2}[-]\\d{1,2}[/]\\d{1,2}[:]\\d{1,2}[:]\\d{1,2}" ).matcher( date ).matches() ) {
				sdf = new SimpleDateFormat( "yyyy-MM-dd/HH:mm:ss" );
				d = sdf.parse( date );
			}
			else if ( Pattern.compile( "^\\d{4}[-]\\d{1,2}[-]\\d{1,2}" ).matcher( date ).matches() ) {
				sdf = new SimpleDateFormat( "yyyy-MM-dd" );
				d = sdf.parse( date );
			}
		}
		catch ( ParseException e ) {
			e.printStackTrace();
		}
		return d;
	}
	
	
	public static boolean isEmpty( String s ){
		return nullto( s ).equals( "" );
	}


	/**
	 * @param args
	 * @throws ParseException
	 */
	public static void main( String[] args ) {
		// Formats.mil(200000);
		// Formats.mil("200000");
		int a_k = 100, b = 10, c = 5, d = 2;
		System.out.println( c / d );
	}

}
